from django.db import models
from django.contrib.auth.models import User, Group, Permission
import urllib
import os.path as path
# Create your models here.


class Series(models.Model):
	ser_title = models.CharField(max_length="100")
	author    = models.ForeignKey(User)
	info      = models.TextField(null=True)
	def __unicode__(self):
		return self.ser_title
		
	def get_absolute_url(self):
		return '/'+unicode(urllib.quote(self.ser_title))+'/'
	class Admin:
		pass
		
class Issue(models.Model):
	ish_title = models.CharField(max_length="100")
	pub_date  = models.DateField()
	series    = models.ForeignKey(Series)
	def __unicode__(self):
		return self.ish_title
	
	def get_absolute_url(self):
		s=Series.objects.get(pk=self.series_id)
		return s.get_absolute_url()+unicode(urllib.quote(self.ish_title))+'/'
		
	def get_cover_url(self):
		front_page = self.page_set.filter(page_number=1)[0]
		return front_page.get_absolute_image_url()
	
	def get_title(self):
		return self.ish_title
	
	class Admin:
		pass
		
class Page(models.Model):
	title       = models.CharField(max_length="100", null=True)
	alt_text    = models.CharField(max_length="200", null=True)
	fle        = models.FileField(upload_to="/home/tyler/media")
	page_number = models.IntegerField()
	pub_date    = models.DateField()
	issue       = models.ForeignKey(Issue)
	
	def __unicode__(self):
		return self.page_number
		
	def get_absolute_image_url(self):
		return '/site-media/'+path.split(self.get_fle_filename())[1]
	
	def get_absolute_url(self):
		i=Issue.objects.get(pk=self.issue_id)
		return i.get_absolute_url()+unicode(self.page_number)+'/'
	class Admin:
		pass
	
	class Meta:
		ordering = ['page_number']
		
class SemiUser(models.Model):
	cookie_code = models.CharField(max_length="105")
	class Admin:
		pass
	def unicode(self):
		return self.cookie_code

class Settings(models.Model):
	user= models.ForeignKey(User)
	def __unicode__(self):
		return self.user
	class Admin:
		pass
		
class Theme(models.Model):
	css_file = models.FilePathField(path="/") #modify this per system
	image_folder = models.FilePathField(path="/") #modify this per system
	users = models.ManyToManyField(Settings)
	def __unicode__(self):
		return u"Theme"
	class Admin:
		pass

	
class BookMarks(models.Model):
	user = models.ForeignKey(Settings, null=True)
	semiuser=models.ForeignKey(SemiUser, null=True)
	class Admin:
		pass
class BookMark(models.Model):
	"""Stores the position in a specific series, issue, and page.
	format: <series name>:<issue name>:page
	"""
	position = models.CharField(max_length="400")
	holder = models.ForeignKey(BookMarks)
	class Admin:
		pass